from django.shortcuts import render,redirect,reverse
from django.http import HttpResponse
from django.views.generic import View,TemplateView
from front.models import Article

class IndexView(View):
    def get(self,request,*args,**kwargs):
        return HttpResponse('get 请求 --类视图')

class BookView(View):
    def get(self,request,book_id):
        data = Article.objects.filter(id=book_id).values('id','title').first()
        print(data)
        return HttpResponse('获取成功')
    # def dispatch(self, request, *args, **kwargs):
    #     print('dispatch')
    #     return super(BookView, self).dispatch(request, *args, **kwargs)

    def http_method_not_allowed(self, request, *args, **kwargs):
        return HttpResponse("不支持get意外的其它请求")


class BookAddView(View):
    def get(self,request,*args,**kwargs):
        return render(request,'add_book.html')

    def post(self,request,*args,**kwargs):
        title = request.POST.get('title')
        content = request.POST.get('content')
        if not Article.objects.filter(title=title).exists():
            Article.objects.create(title=title,content=content)
            return redirect(reverse('index'))
        else:
            return redirect(reverse('add'))


# class AboutView(TemplateView):
#     template_name = 'about.html'
#
#     # def get_context_data(self, **kwargs):
#     #     context = {
#     #         'phone':'18888888888'
#     #     }
#     #     return context